<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class Reserve extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('User/ReserveModel','reserve');
	}


	//列表页
	public function index()
	{	
		//获取教练
		$query = $this->db->query("SELECT id, nickname FROM nmp_coach");
		$data['coach_list'] = $query->result_array();

		//获取场馆
		$query = $this->db->query("SELECT id, name FROM nmp_shop");
		$data['venue_list'] = $query->result_array();

		//
		$weekarray=array("日","一","二","三","四","五","六");
		$data['week'] = "星期".$weekarray[date("w",time())];
		$data['date'] = date('Y年m月d日');

		$this->load->view('reseve_list', $data);
	}


	//获取数据
	public function get_list()
	{		
		$page = $this->input->get('page');
		$page = $page ? $page : 1;
		
		$term['venue_id'] = $this->input->get('venue_id', TRUE);
		$term['coach_id'] = $this->input->get('coach_id', TRUE);
		$term['status'] = $this->input->get('status', TRUE);
		$term['sdate'] = $this->input->get('sdate', TRUE);
		$term['keyword'] = $this->input->get('keyword', TRUE);

		$res = $this->reserve->get_list_data($term,$page);
		
		$this->load->model('User/UserCardModel','user_card');
		foreach($res['list'] as $k=>$v){
			if($v['card_id']){				
				$card = $this->user_card->get_info($v['card_id']);			
				$res['list'][$k]['card_name'] = '';
				if($card){				
					$res['list'][$k]['card_name'] = isset($card['card_name'])?$card['card_name']:'';
				}
			}
		}
		

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//获取教练
	public function get_coach()
	{
		$coach_id = $this->input->get('coach_id');

		$res = $this->reserve->coach_data($coach_id);

		if ($res) 
		{
			$data['nickname'] = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();

	}


	//更新教练
	public function updata_coach()
	{
		$id = $this->input->get('id', TRUE);

		$arr['mid'] = $this->input->get('coach_id', TRUE);

		$res = $this->reserve->updata_coach_data($arr,$id);

		if ($res) 
		{
			$data['status'] = 200;
			$data['message'] = '修改成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '修改失败！';
		}

		echo json_encode($data);exit();

	}


	//更新预约状态
	public function updata_status()
	{
		$id = $this->input->get('id', TRUE);		
		
		$info = $this->reserve->get_info($id);
		if(empty($info)){
			$data['status'] = 400;
			$data['message'] = '数据不存在！';
			echo json_encode($data);exit();
		}
		
		$status = $this->input->get('status', TRUE);
		$arr = array(
			'status' => $status,			
		);		
		$res = $this->reserve->updata_appoint_status($arr,$id);
		
		
		if ($res) 
		{			
			if($status==2){//取消预约，加回次数
				$num = $info['num'];				
				$this->load->model('User/UserCardModel','user_card');
				$card_info = $this->user_card->get_row($info['card_id']);
				$tnum = $card_info['num'];
				$tnum = $num+$tnum;
				
				$sarr = array(
					"num" => $tnum
				);
				$this->user_card->updata_data($sarr,$info['card_id']);
			}
			
			$data['status'] = 200;
			$data['message'] = '更新成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '更新失败！';
		}

		echo json_encode($data);exit();

	}




}



?>